import gdal
import os
import numpy as np
from src.db.db_provider import DBProvider
from src.db.db_agro_sample_management import insert_data_into_tables
from src.utils.loghelper import logHelper
import pandas as pd


class AGROSampleManagement(object):

    def analyze(self, file_path):

        file_list = os.listdir(file_path)

        for file in file_list:

            if "xlsx" in file:

                data = pd.read_excel(file_path + "/" + file, keep_default_na=False)

                for value in data.values:

                    ID = value[0]
                    longitude = value[1]
                    latitude = value[2]
                    year = value[3]
                    doy = value[4]
                    month = value[5]

                    if value[6] == '':
                        class_level1 = None
                    else:
                        class_level1 = value[6]

                    if value[7] == '':
                        class_level2 = None
                    else:
                        class_level2 = value[7]

                    if value[8] == '':
                        class_level3 = 10000
                    else:
                        class_level3 = value[8]

                    sampleSize = value[9]

                    execute_str = insert_data_into_tables["AgroSampleCSV"] % (ID,
                        longitude, latitude, year, doy, month, class_level1, class_level2, class_level3, sampleSize)

                    DBProvider().insert(execute_str)
                    logHelper.info(execute_str)
            else:

                data_set = gdal.Open(file_path + "/" + file)
                data = data_set.ReadAsArray(0, 0, data_set.RasterXSize, data_set.RasterYSize)

                XSize = data_set.RasterXSize
                YSize = data_set.RasterYSize
                geoTransform = data_set.GetGeoTransform()

                x_range = range(0, XSize)
                y_range = range(0, YSize)

                x, y = np.meshgrid(x_range, y_range)
                lon = geoTransform[0] + x * geoTransform[1] + y * geoTransform[2]
                lat = geoTransform[3] + x * geoTransform[4] + y * geoTransform[5]

                observation_year = str(file).split("_")[0]

                try:
                    for i in range(YSize):
                        for j in range(XSize):

                            if data[i][j] > 0:
                                crop_type = data[i][j]
                                longitude = lon[i][j]
                                latitude = lat[i][j]

                                if 10 < crop_type < 20:

                                    class_level1 = 10
                                    class_level2 = 0
                                    class_level3 = 0
                                    if crop_type == 11:
                                        class_level2 = 11
                                        class_level3 = 1

                                    if crop_type == 12:
                                        class_level2 = 12
                                        class_level3 = 2

                                    if crop_type == 13:
                                        class_level2 = 13
                                        class_level3 = 3

                                    if crop_type == 14:
                                        class_level2 = 14
                                        class_level3 = 4

                                    if crop_type == 15:
                                        class_level2 = 15
                                        class_level3 = 5

                                    execute_str = insert_data_into_tables["AgroSample"] % (
                                        longitude, latitude, int(observation_year), class_level1, class_level2, class_level3)

                                    DBProvider().insert(execute_str)
                                    logHelper.info(execute_str)

                except Exception as e:
                    logHelper.warning(e)










